package com.zaofan.mobile.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.zaofan.bean.Comment;
import com.zaofan.bean.Goods;
import com.zaofan.bean.Reply;
import com.zaofan.constant.UrlConstant;
import com.zaofan.service.CommentService;
import com.zaofan.service.GoodsService;
import com.zaofan.service.ReplyService;
import com.zaofan.utils.JsonUtils;

@WebServlet("/mobile/2.0/goods")
public class GoodsController extends BaseServlet {

	private static final long serialVersionUID = 5884947841079014459L;
	private GoodsService goodsService = new GoodsService();
	private CommentService commentService = new CommentService();
	private ReplyService replyService = new ReplyService();

	/**
	 * 产品详情
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void info(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String temp = request.getParameter("id");
			int id = Integer.valueOf(temp);
			Goods p = goodsService.findById(id);
			if (p == null) {
				JsonUtils.writeBeanFailJson("参数错误", response);
				return;
			}
			p.setImage(UrlConstant.HOST_URL + p.getImage());
			p.setBigImage(UrlConstant.HOST_URL + p.getBigImage());
			System.err.println("get product info : id = " + id);
			JsonUtils.writeSuccessJson("查询成功", p, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("参数错误", response);
		}
		JsonUtils.writeBeanFailJson("获取失败", response);
	}

	/**
	 * 获取商家产品
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void goods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String temp = request.getParameter("type");
			int type = -1;
			try {
				type = Integer.valueOf(temp);
			} catch (Exception e) {
				type = -1;
				e.printStackTrace();
			}
			String begin_s = request.getParameter("begin");
			String end_s = request.getParameter("end");
			int begin = 0;
			int end = 10;
			try {
				begin = Integer.valueOf(begin_s);
			} catch (Exception e) {
				begin = 0;
				e.printStackTrace();
			}
			try {
				end = Integer.valueOf(end_s);
			} catch (Exception e) {
				end = 0;
				e.printStackTrace();
			}
			List<Goods> goods = goodsService.findByTypeWithPage(type, begin, end);
			JsonUtils.writeSuccessJson("查询成功", goods, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

	public void comment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String temp = request.getParameter("id");
			int goodsId = -1;
			try {
				goodsId = Integer.valueOf(temp);
			} catch (Exception e) {
				goodsId = -1;
				JsonUtils.writeListFailJson("查询失败!!", response);
				return;
			}
			String begin_s = request.getParameter("begin");
			String end_s = request.getParameter("end");
			int begin = 0;
			int end = 10;
			try {
				begin = Integer.valueOf(begin_s);
			} catch (Exception e) {
				begin = 0;
				e.printStackTrace();
			}
			try {
				end = Integer.valueOf(end_s);
			} catch (Exception e) {
				end = 0;
				e.printStackTrace();
			}
			List<com.zaofan.mobile.bean.Comment> datas = new ArrayList<>();
			com.zaofan.mobile.bean.Comment comment = null;
			List<Comment> comments = commentService.findGoodsComments(goodsId, begin, end);
			for (Comment item : comments) {
				comment = new com.zaofan.mobile.bean.Comment();
				BeanUtils.copyProperties(comment, item);
				if (comment.getIsReply() == 1) {
					Reply reply = replyService.findByCommentId(comment.getId());
					comment.setReply(reply);
				}
				if (comment.getUserPhoto() != null && !"".equals(comment.getUserPhoto())) {
					comment.setUserPhoto(UrlConstant.HOST_URL + comment.getUserPhoto());
				}
				datas.add(comment);
			}
			JsonUtils.writeSuccessJson("查询成功", datas, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

}
